/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author Filib
 */
public class AcaticGps extends MIDlet implements CommandListener {

    private boolean midletPaused = false;
    private Controlador controlador;

    //<editor-fold defaultstate="collapsed" desc=" Generated Fields ">//GEN-BEGIN:|fields|0|
    private Form Inicio;
    private StringItem siLatitud;
    private StringItem siEvento;
    private StringItem siVelocidad;
    private StringItem siLongitud;
    private StringItem PString;
    private StringItem URL;
    private StringItem hora;
    private Form Conf;
    private ChoiceGroup choiceGroup;
    private TextField textField;
    private TextField textField1;
    private TextField textField2;
    private TextField textField3;
    private Command Ocultar;
    private Command Configuracion;
    private Command Guardar;
    private Command itemCommand;
    private Command okCommand;
    //</editor-fold>//GEN-END:|fields|0|

    /**
     * The AcaticGps constructor.
     */
    public AcaticGps() {
    }

    //<editor-fold defaultstate="collapsed" desc=" Generated Methods ">//GEN-BEGIN:|methods|0|
    //</editor-fold>//GEN-END:|methods|0|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: initialize ">//GEN-BEGIN:|0-initialize|0|0-preInitialize
    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {//GEN-END:|0-initialize|0|0-preInitialize
        // write pre-initialize user code here
//GEN-LINE:|0-initialize|1|0-postInitialize
        // write post-initialize user code here
    }//GEN-BEGIN:|0-initialize|2|
    //</editor-fold>//GEN-END:|0-initialize|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: startMIDlet ">//GEN-BEGIN:|3-startMIDlet|0|3-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {//GEN-END:|3-startMIDlet|0|3-preAction
        // write pre-action user code here
        switchDisplayable(null, getInicio());//GEN-LINE:|3-startMIDlet|1|3-postAction
        // write post-action user code here
        controlador = new Controlador();
        controlador.setSiEvento(getSiEvento());
        controlador.setSiLatitud(getSiLatitud());
        controlador.setSiLongitud(getSiLongitud());
        controlador.setSiVelocidad(getSiVelocidad());
        controlador.setSiURL(getURL());
        controlador.setPString(getPString());
        controlador.setHora(getHora());
        controlador.startServicioGPS();
        switchDisplayable(null, getInicio());
    }//GEN-BEGIN:|3-startMIDlet|2|
    //</editor-fold>//GEN-END:|3-startMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: resumeMIDlet ">//GEN-BEGIN:|4-resumeMIDlet|0|4-preAction
    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {//GEN-END:|4-resumeMIDlet|0|4-preAction
        // write pre-action user code here
//GEN-LINE:|4-resumeMIDlet|1|4-postAction
        // write post-action user code here
    }//GEN-BEGIN:|4-resumeMIDlet|2|
    //</editor-fold>//GEN-END:|4-resumeMIDlet|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: switchDisplayable ">//GEN-BEGIN:|5-switchDisplayable|0|5-preSwitch
    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {//GEN-END:|5-switchDisplayable|0|5-preSwitch
        // write pre-switch user code here
        Display display = getDisplay();//GEN-BEGIN:|5-switchDisplayable|1|5-postSwitch
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        } else {
            display.setCurrent(alert, nextDisplayable);
        }//GEN-END:|5-switchDisplayable|1|5-postSwitch
        // write post-switch user code here
    }//GEN-BEGIN:|5-switchDisplayable|2|
    //</editor-fold>//GEN-END:|5-switchDisplayable|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Method: commandAction for Displayables ">//GEN-BEGIN:|7-commandAction|0|7-preCommandAction
    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {//GEN-END:|7-commandAction|0|7-preCommandAction
        // write pre-action user code here
        if (displayable == Conf) {//GEN-BEGIN:|7-commandAction|1|32-preAction
            if (command == Guardar) {//GEN-END:|7-commandAction|1|32-preAction
                // write pre-action user code here
                switchDisplayable(null, getInicio());//GEN-LINE:|7-commandAction|2|32-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|3|28-preAction
        } else if (displayable == Inicio) {
            if (command == Configuracion) {//GEN-END:|7-commandAction|3|28-preAction
                // write pre-action user code here
                switchDisplayable(null, getConf());//GEN-LINE:|7-commandAction|4|28-postAction
                // write post-action user code here
            } else if (command == Ocultar) {//GEN-LINE:|7-commandAction|5|17-preAction
                // write pre-action user code here
                controlador.setEnEjecucionServicioGPS(false);
                try
                {
                    Thread.sleep(2000);
                }
                catch(InterruptedException ex)
                {
                }
                exitMIDlet();//GEN-LINE:|7-commandAction|6|17-postAction
                // write post-action user code here
            }//GEN-BEGIN:|7-commandAction|7|7-postCommandAction
        }//GEN-END:|7-commandAction|7|7-postCommandAction
        // write post-action user code here
    }//GEN-BEGIN:|7-commandAction|8|
    //</editor-fold>//GEN-END:|7-commandAction|8|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Inicio ">//GEN-BEGIN:|15-getter|0|15-preInit
    /**
     * Returns an initiliazed instance of Inicio component.
     * @return the initialized component instance
     */
    public Form getInicio() {
        if (Inicio == null) {//GEN-END:|15-getter|0|15-preInit
            // write pre-init user code here
            Inicio = new Form("AcaticGps", new Item[] { getSiEvento(), getSiLatitud(), getSiLongitud(), getSiVelocidad(), getURL(), getPString(), getHora() });//GEN-BEGIN:|15-getter|1|15-postInit
            Inicio.addCommand(getOcultar());
            Inicio.addCommand(getConfiguracion());
            Inicio.setCommandListener(this);//GEN-END:|15-getter|1|15-postInit
            // write post-init user code here
        }//GEN-BEGIN:|15-getter|2|
        return Inicio;
    }
    //</editor-fold>//GEN-END:|15-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Ocultar ">//GEN-BEGIN:|16-getter|0|16-preInit
    /**
     * Returns an initiliazed instance of Ocultar component.
     * @return the initialized component instance
     */
    public Command getOcultar() {
        if (Ocultar == null) {//GEN-END:|16-getter|0|16-preInit
            // write pre-init user code here
            Ocultar = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|16-getter|1|16-postInit
            // write post-init user code here
        }//GEN-BEGIN:|16-getter|2|
        return Ocultar;
    }
    //</editor-fold>//GEN-END:|16-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: siEvento ">//GEN-BEGIN:|20-getter|0|20-preInit
    /**
     * Returns an initiliazed instance of siEvento component.
     * @return the initialized component instance
     */
    public StringItem getSiEvento() {
        if (siEvento == null) {//GEN-END:|20-getter|0|20-preInit
            // write pre-init user code here
            siEvento = new StringItem("Evento:", null);//GEN-LINE:|20-getter|1|20-postInit
            // write post-init user code here
        }//GEN-BEGIN:|20-getter|2|
        return siEvento;
    }
    //</editor-fold>//GEN-END:|20-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: siLatitud ">//GEN-BEGIN:|21-getter|0|21-preInit
    /**
     * Returns an initiliazed instance of siLatitud component.
     * @return the initialized component instance
     */
    public StringItem getSiLatitud() {
        if (siLatitud == null) {//GEN-END:|21-getter|0|21-preInit
            // write pre-init user code here
            siLatitud = new StringItem("Latitud:", null);//GEN-LINE:|21-getter|1|21-postInit
            // write post-init user code here
        }//GEN-BEGIN:|21-getter|2|
        return siLatitud;
    }
    //</editor-fold>//GEN-END:|21-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: siLongitud ">//GEN-BEGIN:|22-getter|0|22-preInit
    /**
     * Returns an initiliazed instance of siLongitud component.
     * @return the initialized component instance
     */
    public StringItem getSiLongitud() {
        if (siLongitud == null) {//GEN-END:|22-getter|0|22-preInit
            // write pre-init user code here
            siLongitud = new StringItem("Longitud:", null);//GEN-LINE:|22-getter|1|22-postInit
            // write post-init user code here
        }//GEN-BEGIN:|22-getter|2|
        return siLongitud;
    }
    //</editor-fold>//GEN-END:|22-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: siVelocidad ">//GEN-BEGIN:|23-getter|0|23-preInit
    /**
     * Returns an initiliazed instance of siVelocidad component.
     * @return the initialized component instance
     */
    public StringItem getSiVelocidad() {
        if (siVelocidad == null) {//GEN-END:|23-getter|0|23-preInit
            // write pre-init user code here
            siVelocidad = new StringItem("Velocidad:", null);//GEN-LINE:|23-getter|1|23-postInit
            // write post-init user code here
        }//GEN-BEGIN:|23-getter|2|
        return siVelocidad;
    }
    //</editor-fold>//GEN-END:|23-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: URL ">//GEN-BEGIN:|24-getter|0|24-preInit
    /**
     * Returns an initiliazed instance of URL component.
     * @return the initialized component instance
     */
    public StringItem getURL() {
        if (URL == null) {//GEN-END:|24-getter|0|24-preInit
            // write pre-init user code here
            URL = new StringItem("URL:", null);//GEN-LINE:|24-getter|1|24-postInit
            // write post-init user code here
        }//GEN-BEGIN:|24-getter|2|
        return URL;
    }
    //</editor-fold>//GEN-END:|24-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: PString ">//GEN-BEGIN:|25-getter|0|25-preInit
    /**
     * Returns an initiliazed instance of PString component.
     * @return the initialized component instance
     */
    public StringItem getPString() {
        if (PString == null) {//GEN-END:|25-getter|0|25-preInit
            // write pre-init user code here
            PString = new StringItem("PString:", null);//GEN-LINE:|25-getter|1|25-postInit
            // write post-init user code here
        }//GEN-BEGIN:|25-getter|2|
        return PString;
    }
    //</editor-fold>//GEN-END:|25-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: hora ">//GEN-BEGIN:|26-getter|0|26-preInit
    /**
     * Returns an initiliazed instance of hora component.
     * @return the initialized component instance
     */
    public StringItem getHora() {
        if (hora == null) {//GEN-END:|26-getter|0|26-preInit
            // write pre-init user code here
            hora = new StringItem("Hora:", null);//GEN-LINE:|26-getter|1|26-postInit
            // write post-init user code here
        }//GEN-BEGIN:|26-getter|2|
        return hora;
    }
    //</editor-fold>//GEN-END:|26-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Conf ">//GEN-BEGIN:|29-getter|0|29-preInit
    /**
     * Returns an initiliazed instance of Conf component.
     * @return the initialized component instance
     */
    public Form getConf() {
        if (Conf == null) {//GEN-END:|29-getter|0|29-preInit
            // write pre-init user code here
            Conf = new Form("form1", new Item[] { getTextField1(), getTextField2(), getChoiceGroup(), getTextField(), getTextField3() });//GEN-BEGIN:|29-getter|1|29-postInit
            Conf.addCommand(getGuardar());
            Conf.setCommandListener(this);//GEN-END:|29-getter|1|29-postInit
            // write post-init user code here
        }//GEN-BEGIN:|29-getter|2|
        return Conf;
    }
    //</editor-fold>//GEN-END:|29-getter|2|





    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: choiceGroup ">//GEN-BEGIN:|38-getter|0|38-preInit
    /**
     * Returns an initiliazed instance of choiceGroup component.
     * @return the initialized component instance
     */
    public ChoiceGroup getChoiceGroup() {
        if (choiceGroup == null) {//GEN-END:|38-getter|0|38-preInit
            // write pre-init user code here
            choiceGroup = new ChoiceGroup("Menu", Choice.MULTIPLE);//GEN-BEGIN:|38-getter|1|38-postInit
            choiceGroup.append("Envio Inteligente", null);
            choiceGroup.append("Envio en Rafagas", null);
            choiceGroup.append("Intervalo Automatico", null);
            choiceGroup.append("Iniciar al Encender", null);
            choiceGroup.append("Usar Proteccion", null);
            choiceGroup.setFitPolicy(Choice.TEXT_WRAP_DEFAULT);
            choiceGroup.setSelectedFlags(new boolean[] { false, false, false, false, false });//GEN-END:|38-getter|1|38-postInit
            // write post-init user code here
        }//GEN-BEGIN:|38-getter|2|
        return choiceGroup;
    }
    //</editor-fold>//GEN-END:|38-getter|2|





    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Configuracion ">//GEN-BEGIN:|27-getter|0|27-preInit
    /**
     * Returns an initiliazed instance of Configuracion component.
     * @return the initialized component instance
     */
    public Command getConfiguracion() {
        if (Configuracion == null) {//GEN-END:|27-getter|0|27-preInit
            // write pre-init user code here
            Configuracion = new Command("Screen", Command.SCREEN, 0);//GEN-LINE:|27-getter|1|27-postInit
            // write post-init user code here
        }//GEN-BEGIN:|27-getter|2|
        return Configuracion;
    }
    //</editor-fold>//GEN-END:|27-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: Guardar ">//GEN-BEGIN:|31-getter|0|31-preInit
    /**
     * Returns an initiliazed instance of Guardar component.
     * @return the initialized component instance
     */
    public Command getGuardar() {
        if (Guardar == null) {//GEN-END:|31-getter|0|31-preInit
            // write pre-init user code here
            Guardar = new Command("Exit", Command.EXIT, 0);//GEN-LINE:|31-getter|1|31-postInit
            // write post-init user code here
        }//GEN-BEGIN:|31-getter|2|
        return Guardar;
    }
    //</editor-fold>//GEN-END:|31-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: itemCommand ">//GEN-BEGIN:|36-getter|0|36-preInit
    /**
     * Returns an initiliazed instance of itemCommand component.
     * @return the initialized component instance
     */
    public Command getItemCommand() {
        if (itemCommand == null) {//GEN-END:|36-getter|0|36-preInit
            // write pre-init user code here
            itemCommand = new Command("Item", Command.ITEM, 0);//GEN-LINE:|36-getter|1|36-postInit
            // write post-init user code here
        }//GEN-BEGIN:|36-getter|2|
        return itemCommand;
    }
    //</editor-fold>//GEN-END:|36-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: okCommand ">//GEN-BEGIN:|44-getter|0|44-preInit
    /**
     * Returns an initiliazed instance of okCommand component.
     * @return the initialized component instance
     */
    public Command getOkCommand() {
        if (okCommand == null) {//GEN-END:|44-getter|0|44-preInit
            // write pre-init user code here
            okCommand = new Command("Ok", Command.OK, 0);//GEN-LINE:|44-getter|1|44-postInit
            // write post-init user code here
        }//GEN-BEGIN:|44-getter|2|
        return okCommand;
    }
    //</editor-fold>//GEN-END:|44-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField ">//GEN-BEGIN:|49-getter|0|49-preInit
    /**
     * Returns an initiliazed instance of textField component.
     * @return the initialized component instance
     */
    public TextField getTextField() {
        if (textField == null) {//GEN-END:|49-getter|0|49-preInit
            // write pre-init user code here
            textField = new TextField("Ingrese su Contrase\u00F1a", null, 32, TextField.ANY);//GEN-LINE:|49-getter|1|49-postInit
            // write post-init user code here
        }//GEN-BEGIN:|49-getter|2|
        return textField;
    }
    //</editor-fold>//GEN-END:|49-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField1 ">//GEN-BEGIN:|50-getter|0|50-preInit
    /**
     * Returns an initiliazed instance of textField1 component.
     * @return the initialized component instance
     */
    public TextField getTextField1() {
        if (textField1 == null) {//GEN-END:|50-getter|0|50-preInit
            // write pre-init user code here
            textField1 = new TextField("Codigo", "0000", 32, TextField.ANY);//GEN-LINE:|50-getter|1|50-postInit
            // write post-init user code here
        }//GEN-BEGIN:|50-getter|2|
        return textField1;
    }
    //</editor-fold>//GEN-END:|50-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField2 ">//GEN-BEGIN:|51-getter|0|51-preInit
    /**
     * Returns an initiliazed instance of textField2 component.
     * @return the initialized component instance
     */
    public TextField getTextField2() {
        if (textField2 == null) {//GEN-END:|51-getter|0|51-preInit
            // write pre-init user code here
            textField2 = new TextField("Ingrese el Intervalo", "1", 32, TextField.ANY);//GEN-LINE:|51-getter|1|51-postInit
            // write post-init user code here
        }//GEN-BEGIN:|51-getter|2|
        return textField2;
    }
    //</editor-fold>//GEN-END:|51-getter|2|

    //<editor-fold defaultstate="collapsed" desc=" Generated Getter: textField3 ">//GEN-BEGIN:|52-getter|0|52-preInit
    /**
     * Returns an initiliazed instance of textField3 component.
     * @return the initialized component instance
     */
    public TextField getTextField3() {
        if (textField3 == null) {//GEN-END:|52-getter|0|52-preInit
            // write pre-init user code here
            textField3 = new TextField("Ingrese su contrase\u00F1a Nuevamente", null, 32, TextField.ANY);//GEN-LINE:|52-getter|1|52-postInit
            // write post-init user code here
        }//GEN-BEGIN:|52-getter|2|
        return textField3;
    }
    //</editor-fold>//GEN-END:|52-getter|2|

    /**
     * Returns a display instance.
     * @return the display instance.
     */
    public Display getDisplay () {
        return Display.getDisplay(this);
    }

    /**
     * Exits MIDlet.
     */
    public void exitMIDlet() {
        switchDisplayable (null, null);
        destroyApp(true);
        notifyDestroyed();
    }

    /**
     * Called when MIDlet is started.
     * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
     */
    public void startApp() {
        if (midletPaused) {
            resumeMIDlet ();
        } else {
            initialize ();
            startMIDlet ();
        }
        midletPaused = false;
    }

    /**
     * Called when MIDlet is paused.
     */
    public void pauseApp() {
        midletPaused = true;
    }

    /**
     * Called to signal the MIDlet to terminate.
     * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
     */
    public void destroyApp(boolean unconditional) {
    }

}
